package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2022-12-05
 * Time: 15:36
 */
public class AddOperation implements IOperation{

    @Override
    public void Work(BookList bookList) {
        System.out.println("新增图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();
        System.out.println("请输入作者！");
        String author = scanner.nextLine();
        System.out.println("请输入价格！");
        int price = scanner.nextInt();
        System.out.println("请输入类型！");
        String type = scanner.nextLine();
        int curSize = bookList.getUseSize();

        for (int i = 0; i < curSize; i++) {
            Book book = bookList.getBooks(i);
            String tempName = book.getName();
            if (tempName.equals(name)) {
                System.out.println("这本书已存在，不能再添加！");
                return;
            }
        }

        Book book = new Book(name,author,price,type);
        bookList.setBooks(book);
        bookList.setUseSize(curSize + 1);
        System.out.println("书本添加成功！");
    }
}
